package ace.cmp.kaptcha.autoconfigure;

import ace.cmp.kaptcha.core.properties.KaptchaProperties;
import ace.cmp.kaptcha.core.service.KaptchaFactory;
import ace.cmp.kaptcha.core.service.impl.KaptchaFactoryImpl;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author caspar
 * @date 2023/3/4 9:39
 */
@AllArgsConstructor
@EnableConfigurationProperties({KaptchaProperties.class})
@Configuration
public class KaptchaAutoConfigure {
  private final KaptchaProperties properties;

  @Bean
  @ConditionalOnMissingBean
  public DefaultKaptcha defaultKaptcha(KaptchaFactory kaptchaFactory) {
    return kaptchaFactory.create(properties);
  }

  @Bean
  @ConditionalOnMissingBean
  public KaptchaFactory kaptchaFactory() {
    return new KaptchaFactoryImpl();
  }
}
